﻿Imports System.Data.SqlClient

Public Class frmPeriodoFon

    Private Sub frmPeriodoFon_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        btnAbrir.Enabled = False
        btnCerrar.Enabled = False
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim cont As Integer = 0

        Dim strSql As String = ""
        strSql = "Select * From PeriodoFonacot Where Status='Abierto'"

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        txtFinal.Clear()
        txtInicial.Clear()
        txtPeriodo.Clear()
        txtAnio.Clear()

        Dim fecha As Date

        If rdBuscar.Read() Then
            txtPeriodo.Text = rdBuscar("Mes")
            txtInicial.Text = rdBuscar("Del")
            txtFinal.Text = rdBuscar("Al")
            txtAnio.Text = rdBuscar("anio")
            btnCerrar.Enabled = True
        Else
            txtPeriodo.Text = 0
            txtInicial.Text = 0
            txtFinal.Text = 0
            fecha = Now()
            txtAnio.Text = fecha.Year
            btnAbrir.Enabled = True
        End If
    End Sub

    Private Sub btnCerrar_Click(sender As System.Object, e As System.EventArgs) Handles btnCerrar.Click
        Call SetSqlCitra("Update PeriodoFonacot Set Status='Cerrado'")
        btnCerrar.Enabled = False
        txtPeriodo.Text = txtPeriodo.Text + 1
        txtInicial.Text = 0
        txtFinal.Text = 0
        Dim fecha As Date
        fecha = Now()
        txtAnio.Text = fecha.Year
        btnAbrir.Enabled = True
    End Sub

    Private Sub btnAbrir_Click(sender As System.Object, e As System.EventArgs) Handles btnAbrir.Click
        If txtFinal.Text = 0 Then
            MsgBox("No puede haber valores en 0", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If
        If txtInicial.Text = 0 Then
            MsgBox("No puede haber valores en 0", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If
        If txtPeriodo.Text = 0 Then
            MsgBox("No puede haber valores en 0", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If
        If CInt(txtFinal.Text) < CInt(txtInicial.Text) Then
            MsgBox("Los datos son incorrectos", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If

        Dim id As Integer = (BuscarIntDatoCitra("Select Max(IDFonacot) as id from PeriodoFonacot")) + 1
        Dim Info As New clsPeriodoFonacot

        Info.IdFonacot = id
        Info.Mes = txtPeriodo.Text
        Info.Del = txtInicial.Text
        Info.Al = txtFinal.Text
        Info.Anio = txtAnio.Text
        Info.Status = "Abierto"
        Info.Insertar()

        Me.Close()
        Me.Close()

    End Sub

    Private Sub ToolStripButton4_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton4.Click
        Close()

    End Sub
End Class